package com.type;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class Topic extends Photo {
	String title;
	String content;
	ArrayList<Long> followerNumList = new ArrayList<Long>();
	ArrayList<Follower> followList = new ArrayList<Follower>();
	
	
	public Topic() {
		super();
	}
	
	public Topic(String title, String content, Bitmap bitmap) {
		super(bitmap);
		this.title = title;
		this.content = content;
	}
	
	public String getTitle() {
		return title;
	}
	
	public String getContent() {
		return content;
	}
	
	public ArrayList<Follower> getFollowList() {
		return followList;
	}

	@Override
	public void send(DataOutputStream dos) throws IOException, InterruptedException {
		super.send(dos);
		dos.writeUTF(title);
		dos.writeUTF(content);
		
		if(followerNumList.isEmpty())
			dos.writeInt(0);
		else {
			dos.writeInt(followerNumList.size());
			for(Long followerNum : followerNumList)
				dos.writeLong(followerNum.longValue());
		}
	}

	@Override
	public void receive(DataInputStream dis) throws IOException, InterruptedException {
		super.receive(dis);
		title = dis.readUTF();
		content = dis.readUTF();

		int size = dis.readInt();
		if(size > 0) {
			followerNumList.clear();
			for(int i=0; i<size; i++)
				followerNumList.add(new Long(dis.readLong()));
		}
	}
	
	
	public Topic(Parcel in) {
		super(in);
		title = in.readString();
		content = in.readString();
		
		int size = in.readInt();
		if(size > 0) {
			followerNumList.clear();
			for(int i=0; i<size; i++)
				followerNumList.add(new Long(in.readLong()));
		}
	}
	
	public static final Parcelable.Creator<Topic> CREATOR = new Parcelable.Creator<Topic>() {
		public Topic createFromParcel(Parcel in) {
			return new Topic(in);
		}

		@Override
		public Topic[] newArray(int size) {
			return new Topic[size];
		}
	};

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {
		super.writeToParcel(out, flags);
		out.writeString(title);
		out.writeString(content);

		if(followerNumList.isEmpty())
			out.writeInt(0);
		else {
			out.writeInt(followerNumList.size());
			for(Long followerNum : followerNumList)
				out.writeLong(followerNum.longValue());
		}
	}
}
